<template>
  <div class="distribution-recruit-set gray-box top-redius border-bottom-gray">
    <div class="title">{{ $t("recruit.recruitPop") }}</div>
    <el-form
      ref="dataForm"
      :label-width="this.$i18n.t('language') === 'language' ? '160px' : '80px'"
      size="mini"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
    >
      <el-form-item :label="$t('recruit.popularizeSwitch')" prop="state">
        <el-radio-group v-model="dataForm.state">
          <el-radio :label="0">{{ $t("seckill.close") }}</el-radio>
          <el-radio :label="1">{{ $t("seckill.open") }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item :label="$t('recruit.coverImage')">
        <img-upload v-model="dataForm.pic" />
      </el-form-item>

      <el-form-item :label="$t('recruit.popularizeTitle')" prop="title">
        <el-input
          v-model="dataForm.title"
          style="width: 180px"
          maxlength="60"
          show-word-limit
        ></el-input>
      </el-form-item>

      <el-form-item :label="$t('recruit.popularizeContent')" prop="content">
        <tiny-mce
          v-model="dataForm.content"
          style="width: 900px"
          :key="tinymceFlag"
        ></tiny-mce>
      </el-form-item>
    </el-form>
    <el-button @click="dataFormSubmit()">{{
      $t("distribution.save")
    }}</el-button>
  </div>
</template>

<script>
// import PicUpload from '@/components/pic-upload'
import ImgUpload from '@/components/img-upload'
import TinyMce from '@/components/tiny-mce'

export default {
  components: {
    ImgUpload,
    TinyMce
  },
  created () {
    this.getData()
  },
  methods: {
    // 获取数据
    getData () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/distribution/distributionRecruitSet/info'),
        method: 'get'
      }).then(({ data }) => {
        if (data) {
          this.setDate(data)
        }
        this.dataListLoading = false
      })
    },
    // 设置数据
    setDate (data) {
      this.dataForm = data
    },
    // 提交表单
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let param = this.dataForm
          this.$http({
            url: this.$http.adornUrl(`/distribution/distributionRecruitSet`),
            method: 'put',
            data: this.$http.adornData(param)
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500
            })
          }).catch(() => {

          })
        }
      })
    }
  },
  data () {
    return {
      addOrUpdateVisible: false,
      tinymceFlag: new Date() + 1,
      dataForm: {
        'pic': '',
        'title': '',
        'content': ''
      },
      dataRule: {
        title: [
          { required: true, message: this.$i18n.t('recruit.tipTitleTip1'), trigger: 'brue' },
          { max: 255, message: this.$i18n.t('recruit.limitLangTip2'), trigger: 'brue' }
        ],
        content: [
          { required: true, message: this.$i18n.t('product.content'), trigger: 'brue' }
        ]
      }
    }
  },
  // keep-alive缓存的缘故 需调用钩子函数 设置key值不然编辑器无法使用
  activated () {
    this.tinymceFlag = new Date() + 1
  }
}
</script>
<style lang="scss">
.distribution-withdraw-cash-set {
  .input-tips {
    font-size: 12px;
  }
}
</style>
